$(function () {
    var $winWidth = $(window).width();
    //获取头部str
    var gethead = function (data) {
        return '<h1 class="text-center hospital-title">' + data.hosName + '</h1>\n' +
            '    <p class="text-center hospital-adress">' + data.serviceCountry + '</p>\n' +
            '    <div class="hospital-pic">\n' +
            '        <img src="' + data.logoUrl + '" alt="">\n' +
            '    </div>' + getHosImgStr(data.hosImgUrl) + '</div>' + gethosShortedstr(data.hosShorted)
    }
    //获取医院img str
    var getHosImgStr = function (imgstr) {
        var str = '<div class="row">';
        imgstr.split(',').forEach(function (v) {
            str += '<div class="col-xs-6">\n' +
                '<img style="width:100%;" src="' + v + '" alt="">\n' +
                '</div>'
        })
        str += '</div>';
        return str;
    }
    //获取list表str
    var getAllListStr = function (list) {
        var str = '';
        list.forEach(function (v) {
            str += getHospitalStr(v);
        })
        return str;
    }
    //获取单个医生item str
    var getHospitalStr = function (item) {
        return '<div class="hospital-item row">\n' +
            '            <div class="col-xs-12 col-sm-3 imgBox">\n' +
            '                <img img-responsive src="' + item.headUrl + '" alt="">\n' +
            '            </div>\n' +
            '            <div class="col-xs-12 col-sm-9 infoBox">\n' +
            '              <div class="itemlist-right-top">\n' +
            '                <p class="place">' + item.docName + '</p>\n' +
            '                <h3 class="name">' + item.position + '</h3>\n' +
            '               </div>\n'+
            '               <div class="brief">' + gethosShortedstr(item.docShorted) + '</div>\n' +
            '           <img class="listOpenClose" style="cursor: pointer;margin: 15px 0px;" src='+ linkPath + '/images/mechanism/open.png alt="">\n'+
            '            </div>\n' +
            '</div>'
    }
    //获取p标签列表str
    var gethosShortedstr = function (str) {
        return '<div class="shortedBox"><p>' + str.split('##').join('</p><p>') + '</p></div>';
    }

    function load(imgSrc, callback) {
        var imgs = [];
        var c = 0;
        for (var i = 0; i < imgSrc.length; i++) {
            imgs[i] = new Image();
            imgs[i].src = imgSrc[i];
            imgs[i].onload = function () {
                c++
                if (c == imgSrc.length) {
                    if (callback) {
                        callback();
                    }
                }
            }
        }
        return imgs;
    }

    $.getJSON( goBaseUrl + '/songbao/v1/hospital?id=' + hosId, function (data) {
        if(data.Code == '200' && !!data.Data){
            var data = data.Data;
            $('.hospital-head').html(gethead(data));
            if(data.doctorList){
                $('.hospital-box').append(getAllListStr(data.doctorList));
            }

            var $shortedBox = $(".shortedBox");
            var $shortedBoxHeight = $(".shortedBox").height();
            var hiddenHeight = 200;
            if($shortedBoxHeight > hiddenHeight) {
                $shortedBox.addClass('hid');
            }else{
                $('.hospital-detail .openClose').addClass('hide');
            }
            //列表里的图片高度

            $('.hospital-item').each(function (i,v) {
                var $this = $(v);

                load([$this.find('.imgBox img').attr('src')], function () {
                    var $imgHight = $this.find('.imgBox').height();
                    var $itemDesc = $this.find('.shortedBox');
                    var $itemTilteH = $this.find('.itemlist-right-top').height() + 10;
                    var $itemDescHeight  = $itemDesc.height();


                    if($winWidth < 768){
                        $imgHight = 200;
                    }

                    if( ($itemDescHeight + $itemTilteH) > $imgHight){
                        $itemDesc.addClass('hid');
                        $itemDesc.css('max-height',($imgHight- $itemTilteH)  + 'px')
                    }else {
                        $this.find('.listOpenClose').addClass('hide');
                    }
                })

            })


        }
    })

    $(document).on('click',".openClose",function () {
        $(".hospital-head .shortedBox").toggleClass('hid');
        if($(".hospital-head .shortedBox").hasClass('hid')){
            $(".openClose img").attr('src',linkPath + '/images/mechanism/open.png')
        }else{
            $(".openClose img").attr('src',linkPath + '/images/mechanism/close.png')
        }
    })

    $(document).on("click",".listOpenClose",function () {
        var $parent = $(this).parents('.hospital-item');
        var $shortedBox = $parent.find('.shortedBox');
        var $itemTilteH = $parent.find('.itemlist-right-top').height() + 10;
        var $imgHight = $parent.find('.imgBox').height();

        $shortedBox.toggleClass('hid');

        if($winWidth < 768){
            $imgHight = 200;
        }
        if($shortedBox.hasClass('hid')){
            $(this).attr('src',linkPath + '/images/mechanism/open.png')
            $shortedBox.css('max-height',($imgHight - $itemTilteH) + 'px');

        }else{
            $(this).attr('src',linkPath + '/images/mechanism/close.png')
            $shortedBox.css('max-height','inherit');
        }
    })
})
